1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
1 |
|
var floatify = function floatify(str) { |
4
|
236 |
|
var toFloatFormat = function toFloatFormat(str, ts, ds) { |
5
|
182 |
|
var string = str; |
6
|
182 |
|
var decimalSeparator = ds || ''; |
7
|
|
|
|
8
|
182 |
|
string = string.split(ts || '').join(''); |
9
|
182 |
|
if (decimalSeparator !== '') { |
10
|
150 |
|
string = string.split(decimalSeparator).join('.'); |
11
|
|
|
} |
12
|
|
|
|
13
|
182 |
|
return parseFloat(string); |
14
|
|
|
}; |
15
|
|
|
|
16
|
236 |
|
var parseParts = function parseParts(str, ele, count) { |
17
|
124 |
|
var string = str; |
18
|
124 |
|
var element = ele; |
19
|
124 |
|
var parts = string.split(element); |
20
|
|
|
|
21
|
124 |
|
for (var i = 1; i < parts.length; i++) { |
22
|
131 |
|
var left = parts[i - 1]; |
23
|
131 |
|
var leftVal = parseInt(left, 10); |
24
|
|
|
|
25
|
131 |
|
if (parts[i].length === 0) { |
26
|
8 |
|
return Number.NaN; |
27
|
|
|
} |
28
|
|
|
|
29
|
123 |
|
if (parts[i].length === 3) { |
30
|
31 |
|
if (left.length > 3 && parts.length - 1 !== i) { |
31
|
2 |
|
return Number.NaN; |
32
|
|
|
} |
33
|
|
|
|
34
|
29 |
|
if ( |
35
|
|
|
(leftVal === 0 || isNaN(leftVal) || left.length > 3) && parts.length - 1 === i |
36
|
|
|
) { |
37
|
13 |
|
return toFloatFormat(string, '', element); |
38
|
|
|
} |
39
|
|
|
|
40
|
16 |
|
continue; |
41
|
|
|
} |
42
|
|
|
|
43
|
92 |
|
if (i < parts.length - 1) { |
44
|
|
|
// violation in midPart -> NaN |
45
|
12 |
|
return Number.NaN; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// violation in end -> Could be decimalSeparator |
49
|
80 |
|
if (count === 1) { |
50
|
76 |
|
return toFloatFormat(string, '', element); |
51
|
|
|
} |
52
|
|
|
|
53
|
4 |
|
return Number.NaN; |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
return toFloatFormat(string, element, ''); |
57
|
|
|
}; |
58
|
|
|
|
59
|
236 |
|
var parse = function parse(str) { |
60
|
236 |
|
var string = str; |
61
|
|
|
var spacePos; |
62
|
|
|
var spaceSplit; |
63
|
|
|
var spaceCount; |
64
|
|
|
var dotPos; |
65
|
|
|
var commaPos; |
66
|
|
|
var lDotPos; |
67
|
|
|
var lCommaPos; |
68
|
|
|
var dotCount; |
69
|
|
|
var commaCount; |
70
|
|
|
|
71
|
236 |
|
string = string.trim(); |
72
|
|
|
|
73
|
|
|
// 1st dot position |
74
|
236 |
|
dotPos = string.indexOf('.'); |
75
|
|
|
// 1st comma position |
76
|
236 |
|
commaPos = string.indexOf(','); |
77
|
|
|
// 1st space position |
78
|
236 |
|
spacePos = string.indexOf(' '); |
79
|
|
|
|
80
|
236 |
|
if (dotPos + commaPos + spacePos === -3) { |
81
|
|
|
// life is good, no separators |
82
|
18 |
|
return toFloatFormat(string); |
83
|
|
|
} |
84
|
|
|
|
85
|
218 |
|
spaceSplit = string.split(' '); |
86
|
218 |
|
spaceCount = spaceSplit.length - 1; |
87
|
218 |
|
dotCount = string.split('.').length - 1; |
88
|
218 |
|
commaCount = string.split(',').length - 1; |
89
|
|
|
|
90
|
|
|
// only combination of 2 separators allowed |
91
|
218 |
|
if (dotCount > 0 && commaCount > 0 && spaceCount > 0) { |
92
|
1 |
|
return Number.NaN; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// if there is any separator (space, comma, dot) found more than once, |
96
|
|
|
// all other must not be found more than once |
97
|
217 |
|
if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) { |
98
|
4 |
|
return Number.NaN; |
99
|
|
|
} |
100
|
|
|
|
101
|
213 |
|
if (commaCount > 1 && spaceCount > 1) { |
102
|
1 |
|
return Number.NaN; |
103
|
|
|
} |
104
|
|
|
|
105
|
212 |
|
if (spaceCount > 0) { |
106
|
72 |
|
if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) { |
107
|
17 |
|
return Number.NaN; |
108
|
|
|
} |
109
|
55 |
|
string = spaceSplit.join(''); |
110
|
|
|
} |
111
|
|
|
|
112
|
195 |
|
if (dotPos !== -1 && commaPos !== -1) { |
113
|
|
|
// format is using dot and comma |
114
|
|
|
|
115
|
|
|
// last dot position |
116
|
66 |
|
lDotPos = string.lastIndexOf('.'); |
117
|
|
|
// last comma position |
118
|
66 |
|
lCommaPos = string.lastIndexOf(','); |
119
|
|
|
|
120
|
|
|
// order of 1st dot -> comma must be same as last dot -> comma |
121
|
|
|
// 123.123.123,123 -> ok 123.123,123.123 -> not ok |
122
|
66 |
|
if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) { |
123
|
3 |
|
return Number.NaN; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// check positions to guess the thousands separator |
127
|
63 |
|
if (dotPos > commaPos) { |
128
|
56 |
|
if (dotCount > 1) { |
129
|
1 |
|
return Number.NaN; |
130
|
|
|
} |
131
|
|
|
// best guess: . is thousands separator and , is decimal point |
132
|
55 |
|
return toFloatFormat(string, ',', '.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
7 |
|
if (commaCount > 1) { |
136
|
1 |
|
return Number.NaN; |
137
|
|
|
} |
138
|
|
|
// best guess: , is thousands separator and . is decimal point |
139
|
6 |
|
return toFloatFormat(string, '.', ','); |
140
|
|
|
} |
141
|
|
|
|
142
|
129 |
|
if (dotPos !== -1) { |
143
|
|
|
// only dot(s) in format |
144
|
85 |
|
return parseParts(string, '.', dotCount); |
145
|
|
|
} |
146
|
|
|
|
147
|
44 |
|
if (commaPos !== -1) { |
148
|
|
|
// only comma(s) in format |
149
|
39 |
|
return parseParts(string, ',', commaCount); |
150
|
|
|
} |
151
|
|
|
|
152
|
5 |
|
return toFloatFormat(string); |
153
|
|
|
}; |
154
|
|
|
|
155
|
236 |
|
return parse(str); |
156
|
|
|
}; |
157
|
|
|
|
158
|
2 |
|
if (typeof exports !== 'undefined') { |
159
|
4 |
|
if (typeof module !== 'undefined' && module.exports) { |
160
|
1 |
|
exports = module.exports = floatify; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|